package com.xpq.petserver.entity;

import com.xpq.petserver.exception.AppExceptionCodeMsg;
import io.swagger.v3.oas.annotations.media.Schema;

@Schema(name = "统一回复模板")
public class R<T> {
    @Schema(description = "200表示成功，其他表示失败")
    private int code = 200;
    @Schema(description = "提示信息")
    private String msg = "success";
    @Schema(description = "响应数据")
    private T data;

    private R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 业务处理成功时调用返回成功信息
     * @param data 返回数据
     * @param <T>
     * @return
     */
    public static <T> R success(T data) {
        return new R(200, "success", data);
    }

    public static <T> R success(String msg, T data) {
        return new  R(200, msg, data);
    }

    /**
     * 由全局异常处理器调用
     * @param appExceptionCodeMsg 业务异常信息
     * @param <T>
     * @return 统一返回信息
     */
    public static <T> R error(AppExceptionCodeMsg appExceptionCodeMsg) {
        return new R(appExceptionCodeMsg.getCode(), appExceptionCodeMsg.getMsg(), null);
    }

    public static <T> R error(int code, String msg) {
        return new R(code, msg, null);
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
